﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ArtheaEngine.Model
{
    public partial class Flag
    {
        public Flag(long bits)
        {
            Value = bits;
        }

        public Flag() { }

        public static implicit operator long(Flag flag)
        {
            return flag.Value;
        }

        public static implicit operator Flag(long flag)
        {
            return new Flag(flag);
        }

        public override string ToString()
        {
            return Value.ToString();
        }

        public static bool operator == (Flag a, Flag b)
        {
            return (a.Value == b.Value);
        }

        public static bool operator != (Flag a, Flag b)
        {
            return (a.Value != b.Value);
        }

        public static Flag operator !(Flag f)
        {
            return new Flag(f.Value);
        }

        public static Flag operator &(Flag a, Flag b)
        {
            return new Flag(a.Value & b.Value);
        }

        public static Flag operator |(Flag a, Flag b)
        {
            return new Flag(a.Value | b.Value);
        }

        public static Flag operator ~(Flag a)
        {
            return new Flag(~a.Value);
        }

        public static Flag operator ^(Flag a, Flag b)
        {
            return new Flag(a.Value ^ b.Value);
        }

        public static bool operator true(Flag a)
        {
            return a.Value != 0;
        }

        public static bool operator false(Flag a)
        {
            return a.Value == 0;
        }


        public string ToString(Type flaggable)
        {
            if (Value == 0)
                return string.Empty;

            var buf = new StringBuilder();

            foreach (var field in flaggable.GetFields())
            {
                if (field.FieldType != typeof(Flag))
                    continue;

                if (field.GetCustomAttributes(typeof(FlagAttribute), false) == null)
                    continue;

                if (And((Flag)field.GetValue(null)))
                {
                    buf.Append(",");
                    buf.Append(field.Name);
                }
            }

            if (buf.Length > 0)
                buf.Remove(0, 1);

            return buf.ToString();
        }

        public bool And(Flag other)
        {
            return (this & other).Value != 0;
        }

        public void ParseFlags(Type type, string value)
        {
            if (string.IsNullOrEmpty(value))
                return;

            var names = value.Split(',');

            foreach (var name in names)
            {
                var bit =
                    type.GetField(name, BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.Public);

                if (bit == null || bit.GetCustomAttributes(typeof(FlagsAttribute), false) == null)
                    continue;

                Value |= ((Flag)bit.GetValue(null));
            }
        }

        public override bool Equals(object obj)
        {
            try
            {
                return (bool)(this == (Flag)obj);
            }
            catch
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }

        public static Flag MakeBit(int p)
        {
            return new Flag(1L << p);
        }
    }
}
